package com.hhxyjsj;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.alibaba.fastjson.JSONObject;
import com.hhxyjsj.dao.UserInfoMapper;
import com.hhxyjsj.model.UserInfo;

public class MybatisTest {

	public static SqlSessionFactory getSqlSessionFactory(){
		String resource = "mybatis-config.xml";
		InputStream inputStream = null;
		SqlSessionFactory sqlSessionFactory = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);	
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(inputStream != null) inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sqlSessionFactory;
	}
	
	public static SqlSession getSqlSession(SqlSessionFactory factory){
		SqlSession session =  factory.openSession();
		return session;
	}
	
	public static void main(String[] args) {
		SqlSessionFactory factory = getSqlSessionFactory();
		SqlSession session = getSqlSession(factory);
		UserInfoMapper testMapper = session.getMapper(UserInfoMapper.class);
		long id = 3L;
		//通过EncryptionTypeHandler将数据库中存储的idno和Mobile脱敏数据自动还原成原始数据
		UserInfo userInfo = testMapper.selectByPrimaryKey(id);
		System.out.println(JSONObject.toJSONString(userInfo));
		session.close();
	}

}
